package top.suven.http.oauth.vo;


import top.suven.http.oauth.OAuthConstants;

/**
 * 2018-08-01
 * <p/>
 *  OauthAccessToken 信息
 *  客户端	客户详细资料 抽像模块类，用于公共验证和获取客户用户的信息；
 *  将通用的行为(方法) 位于此
 *  grant_type="authorization_code"
 * @author suven.wang
 */

public class OauthAccessToken {
    private static final long serialVersionUID = 7336709167855003668L;


    private long userId;

    private String accessToken;

    private String refreshToken;

    private String tokenType = OAuthConstants.BEARER_TYPE;

    private String clientId;

    private long expiresIn = OAuthConstants.ACCESS_TOKEN_EXPIRED_MS;

    private long refreshTokenExpires;

    private long tokenExpiredMs = OAuthConstants.ACCESS_TOKEN_EXPIRED_MS ;

    private long refreshTokenExpiredMs ;


    public OauthAccessToken() {
    }
    public static OauthAccessToken build(){
        return new OauthAccessToken();
    }

    public boolean tokenExpired() {
        final long time = tokenExpiredMs + OAuthConstants.ACCESS_TOKEN_EXPIRED_MS;
        return time < System.currentTimeMillis();
    }


    public boolean refreshTokenExpired() {
        final long time = refreshTokenExpiredMs + OAuthConstants.REFRESH_TOKEN_EXPIRED_MS;
        return time < System.currentTimeMillis();
    }


    public long currentTokenExpiredSeconds() {
        if (tokenExpired()) {
            return -1;
        }
        final long time = this.tokenExpiredMs - OAuthConstants.ACCESS_TOKEN_EXPIRED_MS;
        return (time - System.currentTimeMillis()) ;
    }

//    public OauthAccessToken updateByClientDetails(OauthClientDetails clientDetails) {
//        this.clientId = clientDetails.getClientId();
//
//        final Integer accessTokenValidity = clientDetails.accessTokenValidity();
//        if (accessTokenValidity != null && accessTokenValidity > 0) {
//            this.tokenExpiredSeconds = accessTokenValidity;
//        }
//
//        final Integer refreshTokenValidity = clientDetails.refreshTokenValidity();
//        if (refreshTokenValidity != null && refreshTokenValidity > 0) {
//            this.refreshTokenExpiredSeconds = refreshTokenValidity;
//        }
//
//        return this;
//    }


    public String clientId() {
        return clientId;
    }

    public OauthAccessToken clientId(String clientId) {
        this.clientId = clientId;
        return this;
    }

    public String refreshToken() {
        return refreshToken;
    }

    public OauthAccessToken refreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
        return this;
    }

    public String tokenType() {
        return tokenType;
    }

    public OauthAccessToken tokenType(String tokenType) {
        this.tokenType = tokenType;
        return this;
    }



    public long getUserId() {
        return userId;
    }

    public OauthAccessToken setUserId(long userId) {
        this.userId = userId;
        return this;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public String getClientId() {
        return clientId;
    }

    public void setClientId(String clientId) {
        this.clientId = clientId;
    }

    public long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(long expiresIn) {
        this.expiresIn = expiresIn;
    }

    public long getRefreshTokenExpires() {
        return refreshTokenExpires;
    }

    public void setRefreshTokenExpires(long refreshTokenExpires) {
        this.refreshTokenExpires = refreshTokenExpires;
    }

    /**
     * Clone
     * Exclude token, refresh_token, authenticationId, expired
     *
     * @return New AccessToken instance
     */
    public OauthAccessToken clone() {
        return new OauthAccessToken()
                .setUserId(userId)
                .clientId(clientId)
                .tokenType(tokenType);
    }
}
